﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wpf_Examples.Views
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        public SettingWindow()
        {
            InitializeComponent();

            // 加载默认语言资源字典 默认中文
            ChangeLanguage(0); // 0 是中文
            combox_Language.SelectedIndex = 0;//下拉第一项为中文
        }


        public void ChangeLanguage(int index)
        {
            Application.Current.Resources.MergedDictionaries.Clear();

            ResourceDictionary rd = new ResourceDictionary();
            switch (index)
            {
                case 0:
                    rd.Source = new Uri("pack://application:,,,/Wpf_Examples;component/Assets/Language/zh-CN.xaml", UriKind.Absolute);
                    break;
                case 1:
                    rd.Source = new Uri("pack://application:,,,/Wpf_Examples;component/Assets/Language/en-US.xaml", UriKind.Absolute);
                    break;
                default:
                    rd.Source = new Uri("pack://application:,,,/Wpf_Examples;component/Assets/Language/en-US.xaml", UriKind.Absolute);
                    break;
            }

            Application.Current.Resources.MergedDictionaries.Add(rd);
        }

        private void combox_Language_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBox = sender as ComboBox;
            if (comboBox != null && comboBox.SelectedItem is ComboBoxItem selectedItem)
            {
                if (int.TryParse(selectedItem.Tag?.ToString(), out int selectedLanguageIndex))
                {
                    ChangeLanguage(selectedLanguageIndex);
                }
            }
        }
    }
}
